package com.idanchuang.component.logback.util;

/**
 * @author yjy
 * Created at 2022/2/21 9:11 下午
 */
public class LogConvertUtil {

    private final static String ENV_KEY = "ACG_CLUSTER";
    private final static String ENV_VALUE_K8S = "K8S";
    private final static boolean IS_IN_K8S = ENV_VALUE_K8S.equals(System.getenv(ENV_KEY));

    /**
     * 当前执行日志打印的appender是否为RollingFileAppender
     * @return ..
     */
    public static boolean isRollingFileAppender() {
        if (IS_IN_K8S) {
            // 如果是k8s环境(不会走ConsoleAppender), 直接返回true, 因为下面这段逻辑可能会比较耗CPU
            return true;
        }
        boolean getAppender = false;
        StackTraceElement[] elements = Thread.currentThread().getStackTrace();
        for (StackTraceElement element : elements) {
            String className = element.getClassName();
            if (className.endsWith("RollingFileAppender")) {
                return true;
            }
            if (className.endsWith("Appender")) {
                getAppender = true;
                continue;
            }
            if (getAppender) {
                break;
            }
        }
        return false;
    }
}
